﻿using PlaneCleanEntity;
using PlaneCleanEntity.ViewEntity.RouterEntity;
using PlaneCleanEntity.ViewEntity.SelectTreeEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PlaneClean.Service
{
    /// <summary>
    /// 菜单相关服务
    /// </summary>
    public class MenuSerivce
    {
        /// <summary>
        /// 根据菜单id检测 菜单id是否存在树中
        /// </summary>
        /// <param name="routerList">所有菜单包括按钮树型结构</param>
        /// <param name="menuIdList">角色权限菜单id包括按钮id</param>
        /// <returns></returns>
        public bool CheckNodeInTree(List<RouterVo> routerList, List<int> menuIdList)
        {
            bool hasPromise = false;
            foreach (var router in routerList)
            {
                if (menuIdList.Contains(router.id))
                {
                    hasPromise = true;
                    break;
                }
                if (router.children != null || router.children.Count > 0)
                {
                    CheckNodeInTree(router.children, menuIdList);
                }
            }
            return hasPromise;
        }
        /// <summary>
        /// 根据菜单id检测 菜单id是否存在树中
        /// </summary>
        /// <param name="menuList">有角色权限的树型菜单列表</param>
        /// <param name="menuId">菜单id</param>
        /// <returns></returns>
        public bool CheckNodeInTree(List<v_t_menu> menuList, int menuId)
        {
            bool inTree = false;
            foreach (var menu in menuList)
            {
                if (menu.m_id == menuId)
                {
                    inTree = true;
                    break;
                }
                if (menu.children != null || menu.children.Count > 0)
                {
                    CheckNodeInTree(menu.children, menuId);
                }
            }
            return inTree;
        }
        /// <summary>
        ///  菜单树
        /// </summary>
        /// <param name="menuList">菜单列表</param>
        /// <param name="pID">第一级菜单父id 0</param>
        /// <returns></returns>
        public List<v_t_menu> ToTreeMenu(List<v_t_menu> menuList, int pID)
        {
            List<v_t_menu> res = new List<v_t_menu>();
            List<v_t_menu> tempList = menuList.Where(c => c.m_parent_id == pID).ToList();
            if (tempList != null && tempList.Count > 0)
            {
                foreach (var temp in tempList)
                {
                    //v_t_menu menu = new v_t_menu();
                    temp.children = ToTreeMenu(menuList, temp.m_id);
                    res.Add(temp);
                }
            }
            return res;
        }
        /// <summary>
        /// 使用递归方法建树 路由相关
        /// </summary>
        /// <param name="routerList">传入list</param>
        /// <param name="pID">第一级菜单父id 0</param>
        /// <returns></returns>
        public List<RouterVo> ToTreeMenu(List<RouterVo> routerList, int pID)
        {
            List<RouterVo> res = new List<RouterVo>();
            List<RouterVo> tempList = routerList.Where(c => c.parentId == pID).ToList();
            if (tempList != null || tempList.Count > 0)
            {
                for (int i = 0; i < tempList.Count; i++)
                {
                    RouterVo router = new RouterVo();
                    router.meta = new RouterMetaVo();
                    router.meta.activeMenu = "";
                    router.meta.affix = false;
                    router.meta.breadcrumb = true;
                    router.meta.icon = tempList[i].meta.icon;
                    router.meta.noCache = false;
                    router.meta.keepAlive = tempList[i].meta.keepAlive;
                    router.meta.path = tempList[i].meta.path;
                    router.meta.roles = tempList[i].meta.roles;//可以查询出来对应的角色占时不用(目前作为前端过滤按钮控制用)
                    router.meta.title = tempList[i].meta.title;

                    router.alwaysShow = tempList[i].alwaysShow;
                    router.component = tempList[i].component;
                    router.hidden = tempList[i].hidden;
                    router.path = tempList[i].path;
                    router.redirect = tempList[i].redirect;
                    router.id = tempList[i].id;
                    router.parentId = tempList[i].parentId;
                    router.name = tempList[i].name;
                    router.children = ToTreeMenu(routerList, router.id);
                    res.Add(router);
                }
            }
            return res;
        }

    }
}